package com.mdp.im.core.subscriber.action.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdp.core.api.Sequence;
import com.mdp.im.core.publish.api.Publisher;
import com.mdp.im.core.scbscriber.api.action.ScbscriberAction;
import com.mdp.im.core.socket.ChannelsHolder;
import com.mdp.im.core.subscriber.action.AbstractSubscriberAction;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 群组创建消息处理器
 * 
 * @author chenyc
 * @date 2018/5/28
 */
@Service
public class ChannelGroupCreatedAction extends AbstractSubscriberAction implements ScbscriberAction {
	
	@Autowired
    Sequence seqService;
	
	@Autowired
	Publisher publisher;
	
    /**
     * 建群操作 
     * 1. 插入数据库 group表，
     * 2. 插入数据库 group_user表
     * 3. 插入im_prichat_message 表 （邀请信息）
     * 3. 发送针对该群各用户的邀请通知
     * 
     * 处理消息
     * 
     * message:{ 
     *  action:'channelGroupCreate',
     *  msgType:'group',
     *  branchId:'归属机构号',
     *  categoryId:'归属分类编号', 
     *  cuserid:'群主编号',
     *  cusername:'群主姓名',
     *  cheadimgurl:'群主头像',
     * 	groupId:'群组编号为空则后台自动生成',
     *  groupName:'群组姓名为空则后台自动生成',
     *  users:{
     *  	userid:'成员编号',
     *      username:'成员姓名',
     *      headimgurl:'成员头像'
     *  }
     * }
     */
    @Override
    public void doAction(JSONObject message) {
        JSONObject json = message;
        if (!json.containsKey("users")) {
            return;
        } 
        String groupId=json.getString("groupId"); 
        String cuserid=json.getString("cuserid"); 
        JSONArray jsonUsers=json.getJSONArray("users"); 
        boolean haveCuserid=false;
        Map<String,Integer> useridOnlineStatus=new HashMap<>();
        for (Object user : jsonUsers) {
        	JSONObject jsonUser=(JSONObject)user;
        	String userid=jsonUser.getString("userid"); 
        	if(userid!=null && userid.equals(cuserid)) {
        		haveCuserid=true;
        	}
            Set<String> channelIdSet = ChannelsHolder.getChannelIdsByUserid(userid);
            if (channelIdSet != null) { 
                for (String imChannelId : channelIdSet) {
                    Channel imChannel = ChannelsHolder.getChannelById(imChannelId);
                    if(imChannel == null) {
                    	 
                        continue;
                    }
                    useridOnlineStatus.put(userid, 1);
                    ChannelsHolder.addChannelToGroup(groupId, imChannel);       
                }
            }else {
            	useridOnlineStatus.put(userid, 0);
            }
        }
        if(haveCuserid==false) { 
            Set<String> channelIdSet = ChannelsHolder.getChannelIdsByUserid(cuserid);
            if (channelIdSet != null) { 
                for (String imChannelId : channelIdSet) {
                    Channel imChannel = ChannelsHolder.getChannelById(imChannelId);
                    if(imChannel == null) {
                        continue;
                    }
                    ChannelsHolder.addChannelToGroup(groupId, imChannel);       
                }
            }
        } 
        
        //groupService.asyncCreateGroupAndBatchInsertUsers(group,groupUsers);  
        super.handleMessageWithGroup(message,true,json.getString("groupId")); 
       
    }

}
